package com.xykj.sdkdemo

import android.view.View
import androidx.recyclerview.widget.GridLayoutManager
import cn.chw.SDK.Entity.FileMsg
import cn.chw.SDK.VEngineSDK
import cn.chw.WaterMark
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.blankj.utilcode.util.ResourceUtils
import com.blankj.utilcode.util.TimeUtils
import com.google.gson.reflect.TypeToken
import com.jhx.library.app.AppActivity
import com.jhx.library.app.Cache.userId
import com.jhx.library.setOnSingleClickListener
import com.jhx.library.util.SuperUtil
import com.xykj.sdkdemo.databinding.ActivityCallingBinding

class CallingActivity : AppActivity<ActivityCallingBinding>() {

    override fun getViewBinding() = ActivityCallingBinding.inflate(layoutInflater)


    val adapter by lazy {
        EventAdapter()
    }


    var sessionId = ""
    var canSpeak = false
    var canHear = false
    var cameraType = 1
    var callId = 0

    override fun initView() {
        /*水印*/
        val waterMark = WaterMark()
        waterMark.longitude = "10.00"
        waterMark.latitude = "20.00"
        waterMark.address = "北京市xxxx"
        waterMark.isDisplayTime = true
        waterMark.remarks = "123456"
        waterMark.userId = "userId"
        VEngineSDK.GetInstance().VEngine_SetWaterMark(waterMark)

        sessionId = intent.getStringExtra("sessionId")!!
        callId = intent.getIntExtra("callId", 0)

        //初始化视图
        VEngineSDK.GetInstance().VEngine_SetSurfaceView(null, binding.s2)

        VEngineSDK.GetInstance().VEngine_Answer(callId, true)

        binding.run {


            recyclerView.layoutManager = GridLayoutManager(this@CallingActivity, 4)
            recyclerView.adapter = adapter

            btTool.setOnClickListener {
                SuperUtil.setVisibility(recyclerView, recyclerView.visibility == View.GONE)
            }

            adapter.setNewInstance(
                GsonUtils.fromJson(
                    ResourceUtils.readAssets2String("item_calling.json"),
                    object : TypeToken<List<String>>() {}.getType()
                )
            )

            adapter.setOnItemClickListener { adapter, view, position ->
                when (position) {
                    0 -> {
                        if (intent.getBooleanExtra("mIsCalling", false)) {
                            //会议发起人，退出会议
                            VEngineSDK.GetInstance().VEngine_DeleteConf_Async(sessionId)
                        } else {
                            //会议成员，挂断会议
                            VEngineSDK.GetInstance().VEngine_Hangup(callId)
                        }
                        finish()
                    }

                    1 -> {
                        val filePath =
                            getLocalFilePath(FileMsg.FileTypePic) + TimeUtils.getNowString() + ".jpg"
                        LogUtils.e(filePath)
                        VEngineSDK.GetInstance().VEngine_CameraTakeSnapshot(filePath)
                        APP.getInstance().runOnMainThread({
                            refreshAlbum(filePath)
                        }, 1500L)
                    }

                    2 -> VEngineSDK.GetInstance().VEngine_ConfAddMember_Async(sessionId, binding.et.text.toString())//邀请

                    3 -> VEngineSDK.GetInstance()//踢出会议
                        .VEngine_ConfDeleteMember_Async(sessionId, binding.et.text.toString())

                    4 -> {//禁言
                        canSpeak = !canSpeak
                        LogUtils.e("禁言 ："+canSpeak)
                        VEngineSDK.GetInstance().VEngine_ConfSetMemberSpeak_Async(
                            sessionId,
                            binding.et.text.toString(),
                            canSpeak
                        )
                    }

                    5 -> {//禁听
                        LogUtils.e("禁听 ："+canHear)
                        canHear = !canHear
                        VEngineSDK.GetInstance().VEngine_ConfSetMemberHear_Async(
                            sessionId,
                            binding.et.text.toString(),
                            canHear
                        )
                    }


                    6 -> VEngineSDK.GetInstance()//推送视频
                        .VEngine_ConfPushMemberVideo_Async(sessionId, binding.et.text.toString())

                    7 -> {
                        if (cameraType == 1){
                            cameraType = 2
                        } else {
                            cameraType = 1
                        }
                        VEngineSDK.GetInstance()//切换摄像头
                            .VEngine_ChangeVideoDevice(callId, cameraType)
                    }

                }

            }


        }

    }
}